package managedbean;

import entity.CustomerEntity;
import entity.PersonEntity;
import entity.SalesLeadEntity;
import entity.SystemUserEntity;
import entity.VendorEntity;
import entity.VendorInvoiceEntity;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.context.Flash;

@SessionScoped
public abstract class AbstractBacking implements Serializable {

    @EJB
    public bean.session.SystemUserEntityFacade systemUserEntityFacade;
    @EJB
    public bean.session.VendorInvoiceEntityFacade viFacade;
    String fileToDownloadPath;

    public SystemUserEntity getCurrentUser() {
        SystemUserEntity u = (SystemUserEntity) getFacesContext().getExternalContext().getSessionMap().get("currentUser");
        if (u == null || u.getId() == null) {
            return u;
        } else {
            return systemUserEntityFacade.find(u.getId()); //get the lasted version of the user
        }
    }

    public void setVendorInvoice(VendorInvoiceEntity vi) {
        if (null != vi) {
            getFacesContext().getExternalContext().getSessionMap().put("vendorInvoice", vi);
        }
    }
     public VendorInvoiceEntity getVendorInvoice() {
        VendorInvoiceEntity  u = (VendorInvoiceEntity ) getFacesContext().getExternalContext().getSessionMap().get("vendorInvoice");
        if (u == null || u.getId() == null) {
            return u;
        } else {
            return viFacade.find(u.getId()); //get the lasted version of the user
        }
    }

    public void setCurrentUser(SystemUserEntity user) {
        if (null != user) {
            getFacesContext().getExternalContext().getSessionMap().put("currentUser", user);
        }
    }

    public CustomerEntity getLoginCustomer() {
        return (CustomerEntity) getFacesContext().getExternalContext().getSessionMap().get("loginCustomer");
    }

    public void setLoginCustomer(CustomerEntity currentCustomer) {
        if (null != currentCustomer) {
            getFacesContext().getExternalContext().getSessionMap().put("loginCustomer", currentCustomer);
        }
    }

    public SalesLeadEntity getLoginSalesLead() {
        return (SalesLeadEntity) getFacesContext().getExternalContext().getSessionMap().get("loginSalesLead");
    }

    public void setLoginSalesLead(SalesLeadEntity currentSalesLead) {
        if (null != currentSalesLead) {
            getFacesContext().getExternalContext().getSessionMap().put("loginSalesLead", currentSalesLead);
        }
    }

    public VendorEntity getLoginVendor() {
        return (VendorEntity) getFacesContext().getExternalContext().getSessionMap().get("loginVendor");
    }

    public void setLoginVendor(VendorEntity vendor) {
        if (null != vendor) {
            getFacesContext().getExternalContext().getSessionMap().put("loginVendor", vendor);
        }
    }

    public PersonEntity getLoginPerson() {
        PersonEntity u = (PersonEntity) getFacesContext().getExternalContext().getSessionMap().get("loginPerson");
        if (u == null || u.getId() == null) {
            return u;
        } else {
            return systemUserEntityFacade.getPersonById(u.getId()); //get the lasted version of the user
        }
    }

    public void setLoginPerson(PersonEntity person) {
        if (null != person) {
            getFacesContext().getExternalContext().getSessionMap().put("loginPerson", person);
        }
    }

    public SystemUserEntity refreshAndFetchCurrentUser() {
        SystemUserEntity u = systemUserEntityFacade.find(getCurrentUser().getId());
        setCurrentUser(u);
        return getCurrentUser();
    }

    public boolean isUserLoggedIn() {
        PersonEntity u = (PersonEntity) getFacesContext().getExternalContext().getSessionMap().get("loginPerson");
        return u != null;
    }

    public FacesContext getFacesContext() {
        return FacesContext.getCurrentInstance();
    }

    public Flash getFlash() {
        return getFacesContext().getExternalContext().getFlash();
    }

    public String getFileToDownloadPath() {
        return fileToDownloadPath;
    }

    public void setFileToDownloadPath(String fileToDownloadPath) {
        this.fileToDownloadPath = fileToDownloadPath;
    }
    
}
